package com.eu.ebpm.engine.cmd;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.eu.ebpm.engine.entity.args.Param;
import com.eu.ebpm.engine.entity.deploy.Mission;
import com.eu.ebpm.engine.exception.ErrorCode;
import com.eu.ebpm.engine.exception.WorkFlowException;
import com.eu.ebpm.engine.init.EbpmCache;
import com.eu.ebpm.engine.pvm.EngineServiceType;
import com.eu.ebpm.engine.repository.ForkVariableRepository;
import com.eu.ebpm.engine.repository.TaskRepository;
import com.eu.ebpm.engine.service.Task;

/**
 * 签收回退
 * @author 王晓华
 */
@Service
public class RevokeTaskSignCommand {

	@Autowired
	private TaskIdentitySupport taskIdentityService;
	
	@Autowired
	private TaskRepository taskRepository;
	
	@Autowired
	private ForkVariableRepository forkVariableRepository;
	
	public void execute(Task task) {
		
		Mission mission = EbpmCache.getMission(task.getMissionId());
		
		if(mission.getType() == EngineServiceType.LANE_TASK) {
			throw new WorkFlowException(ErrorCode.RU_UNSUPORT_LANETASK_UNSIGN);
		}
		
		long result = taskRepository.updateBySign(null, null, task.getId());
		
		if(result <= 0) {
			throw new WorkFlowException(ErrorCode.RU_BACK_SIGN_ERROR);
		}
		
		forkVariableRepository.delete(Param.map("taskId", task.getId()));
		
		taskIdentityService.insertTaskCandidate(mission, task);
		
	}

}
